<?php
namespace Wx\Controller;
use Think\Controller;

class UserController extends CommonController {

    public function user(){
//        if(session('u_type')==0)$this->redirect('User/index');//普通
//        if(session('u_type')==1)$this->redirect('User/stores');//门店啊啊啊啊
//        if(session('u_type')==2)$this->redirect('User/farmers');//农户
		$this->redirect('User/index');
    }//end fun

    //普通用户
    public function index(){
		session('pro_url',null);
        $member = M('member');
        $u_id = session('u_id');
        if ($u_id){
            $member_info = $member->where("status != 1 AND id= {$u_id}")->find();
            if (!$member_info['hometown']){
                $member_info['hometown'] = '未绑定';
            }

            $order =  M('shop_spellgroup_order');
            // 拼团各个状态订单得数量
            $data['order0'] = $order->where(array('u_id'=>session('u_id'),'state'=>0))->count();    //待付款数量
            $data['order1'] = $order->where(array('u_id'=>session('u_id'),'state'=>1))->count();    //待成团数量
            $data['order2'] = $order->where(array('u_id'=>session('u_id'),'state'=>2))->count();    //待评价数量
            $data['order4'] = $order->where(array('u_id'=>session('u_id'),'state'=>4))->count();    //待发货数量
            $data['order6'] = $order->where(array('u_id'=>session('u_id'),'state'=>6))->count();    //待核销数量
//            dump($data);
            //活动各种状态订单数量
            $data['f_order0'] = M("foretaste_order")->where("u_id = ".session('u_id')." AND state in(0,1,7,6)")->count();    //申请状态数量
            $data['f_order1'] = M("foretaste_order")->where("u_id = ".session('u_id')." AND state = 2")->count();    //已发货状态数量
            $data['f_order2'] = M("foretaste_order")->where("u_id = ".session('u_id')." AND state = 3")->count();    //待核销状态数量
            $data['f_order3'] = M("foretaste_order")->where("u_id = ".session('u_id')." AND state = 4")->count();    //待评价状态数量
			//我的兑换
            $data['dh_order0'] = M("exchange_order")->where("u_id = ".session('u_id')." AND state = 0")->count();    //代付款
            $data['dh_order1'] = M("exchange_order")->where("u_id = ".session('u_id')." AND state = 1")->count();    //待核销
            $data['dh_order2'] = M("exchange_order")->where("u_id = ".session('u_id')." AND state = 2")->count();    //已核销
            $data['dh_order3'] = M("exchange_order")->where("u_id = ".session('u_id')." AND state = 3")->count();    //已评价
            // 收藏量
            $data['collection'] = M('shop_spellgroup_collection')->where(array('u_id'=>session('u_id')))->count();

            // 消息数量
            $voice_num = M('voice')->where(array('u_id'=>session('u_id'),'status'=>0))->count();
            $comment_num = M('evaluate_comment')->where(array('accept_id'=>session('u_id'),'status'=>0))->count();
            $data['total_num'] = intval($voice_num)+intval($comment_num);
//            dump($data['total_num']);
            $this->assign('member',$member_info);
            $this->assign($data);
			
		//检测该用户是否有成功的拼团或者试吃，且该小区没有门店
			$foretaste_order=D('foretaste_order');//试吃试用订单表
			$shop_spellgroup_order=D('shop_spellgroup_order');//拼团订单表
			
			$havegood=0;//默认无
			$getfo=$foretaste_order->where("state=1 and u_id=".session('u_id'))->find();
			$getso=$shop_spellgroup_order->where("state=4 and u_id=".session('u_id'))->find();
			if($getfo || $getso)$havegood=1;//有成功的拼团或者试吃
			
			$havestore=0;//默认无店铺
			$shop_store=D('shop_store');
			if($member_info['c_id']>0)$getsp=$shop_store->where("c_id =".$member_info['c_id'])->find();
			if($getsp)$havestore=1;//有店铺
			
			//检测是否推荐过店铺
			$rec_s=0;//未推荐过
			$shop_recommend=D('shop_recommend');
			$getsr=$shop_recommend->where("u_id=".session('u_id'))->find();
			if($getsr)$rec_s=1;//推荐过店铺
			
			if($havegood==1 && $havestore==0 && session('recommend')!=1 && $rec_s==0){
				$this->assign('recommend',0);//弹出推荐店铺
				session('recommend',1);//弹过一次
			}else{
				$this->assign('recommend',1);//不弹出推荐店铺
			}
		//检测该用户是否有成功的拼团或者试吃，且该小区没有门店-end 
			
            $this->display('usercenter');
        }
    }//end fun

    //商户
    public function stores(){
        $member = M('member');
        $store = M("shop_store");
        $u_id = session('u_id');
        $member_info = $member->where("status != 1 AND id= {$u_id}")->find();
        $store_info = $store->where("status != 0 AND u_id= {$u_id}")->find();
        $this->assign('member',$member_info);
        $this->assign('store',$store_info);
		//今日收益
			$member_credit_log=D('member_credit_log');
			$year = date("Y");
			$month = date("m");
			$day = date("d");
			$start = mktime(0,0,0,$month,$day,$year);//当天开始时间戳
			$end= mktime(23,59,59,$month,$day,$year);//当天结束时间戳
			$credit_total=$member_credit_log->where("u_id=".$u_id." and credit_type like 'credit3' and shop_credit=0 and (`change_type` LIKE '+') and (changetime>".$start." and changetime<".$end.") ")->order("id desc")->sum('change_number');
			if($credit_total==null)$credit_total=0;
			$credit_total_md=$member_credit_log->where("u_id=".$u_id." and credit_type like 'credit3' and shop_credit=1 and (`change_type` LIKE '+') and (changetime>".$start." and changetime<".$end.") ")->order("id desc")->sum('change_number');
			if($credit_total_md==null)$credit_total_md=0;
			$credit_total_jf=$member_credit_log->where("u_id=".$u_id." and credit_type like 'credit3' and shop_credit=3 and (`change_type` LIKE '+') and (changetime>".$start." and changetime<".$end.") ")->order("id desc")->sum('change_number');
			if($credit_total_jf==null)$credit_total_jf=0;
			$credit_total_xc=$member_credit_log->where("u_id=".$u_id." and credit_type like 'credit3' and shop_credit=2 and (`change_type` LIKE '+') and (changetime>".$start." and changetime<".$end.") ")->order("id desc")->sum('change_number');
			if($credit_total_xc==null)$credit_total_xc=0;
			
			$this->assign('credit_total',$credit_total);//线下收银
			$this->assign('credit_total_md',$credit_total_md);//门店拼团
			$this->assign('credit_total_jf',$credit_total_jf);//积分兑换
			$this->assign('credit_total_xc',$credit_total_xc);//乡村拼团
		//今日收益-end
		//统计-start
		//拼团发布
            $data['pt_order0'] = M("shop_spellgroup")->where("u_id = ".session('u_id')." AND state in(0,2)")->count();    //审核中
            $data['pt_order1'] = M("shop_spellgroup")->where("u_id = ".session('u_id')." AND state = 1")->count();    //拼团中
            $data['pt_order2'] = M("shop_spellgroup")->where("u_id = ".session('u_id')." AND state = 4")->count();    //结束
		//兑换管理
            $data['dh_order0'] = M("exchange_goods")->where("u_id = ".session('u_id')." AND state in(0,2)")->count();    //审核中
            $data['dh_order1'] = M("exchange_goods")->where("u_id = ".session('u_id')." AND state = 1")->count();    //已上架
            $data['dh_order2'] = M("exchange_goods")->where("u_id = ".session('u_id')." AND state = 4")->count();    //已下架
			$c_id=M('shop_store')->where("u_id = ".session('u_id'))->getField('c_id');//dump($c_id);
            $data['dh_order3'] = M("exchange_order")->where("c_id = ".$c_id." AND state =0")->count();    //代付款
            $data['dh_order4'] = M("exchange_order")->where("c_id = ".$c_id." AND state = 1")->count();    //待核销
            $data['dh_order5'] = M("exchange_order")->where("c_id = ".$c_id." AND state = 2")->count();    //已核销
            $data['dh_order6'] = M("exchange_order")->where("c_id = ".$c_id." AND state = 3")->count();    //已经评价
		//订单管理
        $s_id = session("s_id");
        $store = M("shop_store");
        $spellgroup_copy = "xtws_shop_spellgroup_copy"; //拼团副本-数据库表名
        $spellgroup = "xtws_shop_spellgroup";//拼团商品-数据库表名
        $spellgroup_order = "xtws_shop_spellgroup_order";//拼团订单-数据库表名
        	//农户拼团待收货信息
        $data['dd_order0'] = $store
            ->alias('store')
            ->join("{$spellgroup_copy} s_copy ON s_copy.c_id = store.c_id")
            ->join("{$spellgroup} spell ON spell.id = s_copy.sg_id")
            ->where("store.id = {$s_id} AND s_copy.orsuccess = 1 AND s_copy.state = 2 AND spell.type = 0 ") 
            //->group("s_copy.id desc")
            ->order("s_copy.id desc")
            ->field('spell.*,s_copy.num AS s_copy_num,s_copy.id as copy_id,s_copy.num as copy_num')
            ->count();
        	//门店拼团待成团商品信息
        $data['dd_order1'] = $store
            ->alias('store')
            ->join("{$spellgroup} spell ON spell.s_id = store.id")
            ->join("{$spellgroup_copy} s_copy ON s_copy.sg_id = spell.id")
            ->where("store.id = {$s_id} AND s_copy.orsuccess = 0 AND s_copy.state = 0 AND spell.type = 1 ") 
            //->group("s_copy.id desc")
            ->order("s_copy.id desc")
            ->field('spell.*,s_copy.num AS s_copy_num,s_copy.id as copy_id,s_copy.num as copy_num')
            ->count();
			
        	//门店拼团待通知商品信息
        $store_tz = $store
            ->alias('store')
            ->join("{$spellgroup} spell ON spell.s_id = store.id")
            ->join("{$spellgroup_copy} s_copy ON s_copy.sg_id = spell.id")
            ->where("store.id = {$s_id} AND s_copy.orsuccess = 1 AND s_copy.state = 3 AND spell.type = 1 ") 
            //->group("s_copy.id desc")
            ->order("s_copy.id desc")
            ->field('spell.*,s_copy.num AS s_copy_num,s_copy.id as copy_id,s_copy.num as copy_num')
            ->count();
        	//农户拼团待通知商品信息
        $store_tz_xc = $store
            ->alias('store')
            ->join("{$spellgroup_copy} s_copy ON s_copy.c_id = store.c_id")
            ->join("{$spellgroup} spell ON spell.id = s_copy.sg_id")
            ->where("store.id = {$s_id} AND s_copy.orsuccess = 1 AND s_copy.state = 3 AND spell.type = 0 ") 
            //->group("s_copy.id desc")
            ->order("s_copy.id desc")
            ->field('spell.*,s_copy.num AS s_copy_num,s_copy.id as copy_id,s_copy.num as copy_num')
            ->count();
        $data['dd_order3'] = $store_tz+$store_tz_xc;
		//我的进货
            $data['jh_order0'] = M("wholesale")->where("u_id = ".session('u_id')." AND state =0")->count();    //代付款
            $data['jh_order1'] = M("wholesale")->where("u_id = ".session('u_id')." AND state = 2")->count();    //代发货
            $data['jh_order2'] = M("wholesale")->where("u_id = ".session('u_id')." AND state = 4")->count();    //已发货
		//统计-end
		 
            $this->assign($data);
        $this->display('usercenter-sj');
    }//end fun

    //农户
    public function farmers(){
        $member = M('member');
        $u_id = session('u_id');
        $spellgroup = M("shop_spellgroup");
        $spellgroup_copy = "xtws_shop_spellgroup_copy";
        $wholesale = "xtws_wholesale";
        $member_info = $member->where("status != 1 AND id= {$u_id}")->find();
        // 消息数量
        $voice_num = M('voice')->where(array('u_id'=>session('u_id'),'status'=>0))->count();
        $comment_num = M('evaluate_comment')->where(array('accept_id'=>session('u_id'),'status'=>0))->count();
        $data['total_num'] = intval($voice_num)+intval($comment_num);
        // 产品状态数量
        $pro0 = $spellgroup->where("state = 0 AND u_id = {$u_id}")->count();   // 待审
        $pro2 = $spellgroup->where("state = 2 AND u_id = {$u_id}")->count();   // 不合格
        $data['pro0'] = intval($pro0)+intval($pro2);    //待审+不合格
        $data['pro1'] = $spellgroup->where("state = 1 AND u_id = {$u_id}")->count();   // 拼团中
        $data['pro4'] = $spellgroup->where("state = 4 AND u_id = {$u_id}")->count();   // 结束

        // 各个订单状态数量
        $data['pf_state0'] = $spellgroup
                            ->alias('sg')
                            ->join("{$wholesale} wh ON sg.id = wh.g_id")
                            ->where("sg.u_id = {$u_id} AND wh.state = 3")
                            ->count();  //待付款-批发
        $data['state1'] = $spellgroup
                            ->alias('sg')
                            ->join("{$spellgroup_copy} sc ON sg.id = sc.sg_id")
                            ->where("sg.u_id = {$u_id} AND sg.state = 4 AND sc.state = 1 AND sc.orsuccess = 1")
                            ->count();  //待发货
        $data['pf_state1'] = $spellgroup
                            ->alias('sg')
                            ->join("{$wholesale} wh ON sg.id = wh.g_id")
                            ->where("sg.u_id = {$u_id} AND sg.state = 4 AND wh.state = 2")
                            ->count();  //待发货-批发
        $data['state2'] = $spellgroup
                            ->alias('sg')
                            ->join("{$spellgroup_copy} sc ON sg.id = sc.sg_id")
                            ->where("sg.u_id = {$u_id} AND sg.state = 4 AND sc.state = 2 AND sc.orsuccess = 1")
                            ->count();  //已发货
        $data['pf_state2'] = $spellgroup
                            ->alias('sg')
                            ->join("{$wholesale} wh ON sg.id = wh.g_id")
                            ->where("sg.u_id = {$u_id} AND wh.state = 4")
                            ->count();  //已发货-批发
        $data['state3'] = $spellgroup
                            ->alias('sg')
                            ->join("{$spellgroup_copy} sc ON sg.id = sc.sg_id")
                            ->where("sg.u_id = {$u_id} AND sg.state = 4 AND sc.state >= 3 AND sc.orsuccess = 1")
                            ->count();  //已收货
        $data['pf_state3'] = $spellgroup
                            ->alias('sg')
                            ->join("{$wholesale} wh ON sg.id = wh.g_id")
                            ->where("sg.u_id = {$u_id} AND wh.state = 5")
                            ->count();  //已收货-批发
        $data['pf_state4'] = $spellgroup
            ->alias('sg')
            ->join("{$wholesale} wh ON sg.id = wh.g_id")
            ->where("sg.u_id = {$u_id} AND wh.state = 6")
            ->count();  //已评价-批发

        $this->assign('member',$member_info);
        $this->assign($data);
        $this->display('usercenter-nh');
    }//end fun

    //切换用户
    public function change(){
        $type = I("get.type");
        if(session('u_type')==1)$this->redirect('User/stores');//门店
        if(session('u_type')==2)$this->redirect('User/farmers');//农户
        if ($type){
            if($type == 1){
                if(session('u_type')==1){
                    $this->redirect('User/stores');
                }else{
                    $this->redirect('No/index',['typename'=>'商户']);
                }//门店
            }elseif($type == 2){
                if(session('u_type')==2){
                    $this->redirect('User/farmers');
                }else{
                    $this->redirect('No/index',['typename'=>'农户']);
                }//农户
            }
        }
        $this->display('change');
    }//end fun

    // 我的积分
    public function integration(){
        // 积分值
        $data['total'] = M('member')->where('id = '.session('u_id'))->getField('credit1');
        //积分变动明细
        $data['list'] = M('member_credit_log')->where(array('u_id'=>session('u_id'),'credit_type'=>'credit1'))->order('id desc')->select();
        $this->assign($data);
        $this->display();
    }//end fun integration()

    // 积分规则
    public function rule(){
        $this->display();
    }//end fun rule()

    // 我的卡额
    public function balance(){
        // 积分值
        $data['total'] = M('member')->where('id = '.session('u_id'))->getField('credit2');
        //积分变动明细
        $data['list'] = M('member_credit_log')->where(array('u_id'=>session('u_id'),'credit_type'=>'credit2'))->order('id desc')->select();
        $this->assign($data);
        $this->display();
    }//end fun balance()

    // 我的余额
    public function credit3(){
        // 积分值
        $data['total'] = M('member')->where('id = '.session('u_id'))->getField('credit3');
        //积分变动明细
        $data['list'] = M('member_credit_log')->where(array('u_id'=>session('u_id'),'credit_type'=>'credit3'))->order('id desc')->select();
        $this->assign($data);
        $this->display();
    }//end fun balance()

    //充值
    public function recharge(){
        if(IS_POST){
            $post['total_fee'] = I('on');   //接收用户选择的充值金额
            if($post['total_fee']!='100.00'&&$post['total_fee']!='200.00'&&$post['total_fee']!='500.00'&&$post['total_fee']!='1000.00'){
                $this->error('金额错误，请重新选择');
            }
            $post['u_id'] = session('u_id');
            $post['order_sn'] = 'Re'.date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT); //订单编号
            $post['addtime'] = time();
            $result = M('recharge_log')->add($post);     //新增数据id
            // 组合url
            $url = 'http://www.xiangtws.com/Api/Weixinpay/recharge?order_id='.$result;
            // 前往支付
            header('location:'.$url);
        }else{
            $this->display();
        }

    }//end fun recharge()


}//end class